﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class styles_MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserLogin user = (UserLogin)Session["User"];

        if (user != null)
        {
            tAccount.Style["display"] = "block";
            tRegis.Style["display"] = "none";

            lnkProfile.InnerText = user.Name;
            lnkLog.Text = "Logout";            
        }
        else
        {
            tAccount.Style["display"] = "none";
            tRegis.Style["display"] = "block";

            lnkLog.Text = "Login";
            lnkLog.PostBackUrl = "../UserLogin.aspx";           
        }

        if (!IsPostBack)
        {
            Sharemp3DataContext db = new Sharemp3DataContext();
            Rpt_Genres.DataSource = db.Genres.OrderBy(i => i.GenresName);
            Rpt_Genres.DataBind();
        }        
    }

    protected void Search(object sender, EventArgs e)
    {
        string key = txtKey.Text;
        searhBoxTitle.InnerText = "Search with \"" + key + "\"";
        if (key != null && key.Length > 0)
        {            
            using (Sharemp3DataContext db = new Sharemp3DataContext())
            {
                var artists = from i in db.Artists
                              where i.ArtistName.Contains(key)
                              select new
                              {
                                  ArtistID = i.ArtistID,
                                  ArtistName = i.ArtistName,
                                  ImageCover = i.ImageCover
                              };

                if (artists.Count() != 0)
                {
                    Rpt_Artist_m.DataSource = artists;
                    Rpt_Artist_m.DataBind();
                    artist_mdiv.Style["display"] = "block";
                }
                else
                {
                    artist_mdiv.Style["display"] = "none";
                }                

                var albums = from i in db.Albums
                             where i.AlbumName.Contains(key)
                             select new
                             {
                                 AlbumID = i.AlbumID,
                                 AlbumName = i.AlbumName,
                                 ArtistID = i.ArtistID,
                                 ArtistName = i.Artist.ArtistName,
                                 AlbumCover = i.AlbumCover
                             };

                if (albums.Count() != 0)
                {
                    Rpt_Album_m.DataSource = albums;
                    Rpt_Album_m.DataBind();
                    album_mdiv.Style["display"] = "block";
                }
                else
                {
                    album_mdiv.Style["display"] = "none";
                }

                var videolist = from i in db.VideoClips
                             where i.Title.Contains(key)
                             select new
                             {
                                 VideoID = i.VideoID,
                                 Title = i.Title,
                                 ArtistID = i.Album.ArtistID,
                                 ArtistName = i.Album.Artist.ArtistName,
                                 Poster = i.Poster
                             };

                if (videolist.Count() != 0)
                {
                    Rpt_Video_m.DataSource = videolist;
                    Rpt_Video_m.DataBind();
                    video_mdiv.Style["display"] = "block";
                }
                else
                {
                    video_mdiv.Style["display"] = "none";
                }

                var music = from i in db.Musics
                            where i.Title.Contains(key)
                            select new
                            {
                                MusicID = i.MusicID,
                                Title = i.Title,
                                ArtistID = i.Album.ArtistID,
                                ArtistName = i.Album.Artist.ArtistName,
                                Cover = i.Cover
                            };

                if (music.Count() != 0)
                {
                    Rpt_Music_m.DataSource = music;
                    Rpt_Music_m.DataBind();
                    music_mdiv.Style["display"] = "block";
                }
                else
                {
                    music_mdiv.Style["display"] = "none";
                }
            }            
        }
        else
        {
            Rpt_Artist_m.DataSource = null;
            Rpt_Artist_m.DataBind();
            artist_mdiv.Style["display"] = "none";
            Rpt_Album_m.DataSource = null;
            Rpt_Album_m.DataBind();
            album_mdiv.Style["display"] = "none";
            Rpt_Video_m.DataSource = null;
            Rpt_Video_m.DataBind();
            video_mdiv.Style["display"] = "none";
            Rpt_Music_m.DataSource = null;
            Rpt_Music_m.DataBind();
            music_mdiv.Style["display"] = "none";
        }
        SearchResultPanel.Style["display"] = "block";
        SearchResultPanel.Focus();
    }

    protected void Logout(object sender, EventArgs e)
    {
        Session["User"] = null;
        Response.Redirect(Request.RawUrl);
    }
}
